import utils from './utils.js'
import tools from './tools.js';

process.stdin.on('data', (data) => {
  if (!data) return;

  let req;
  try {
    req = JSON.parse(data);
  } catch (e) {
    const errorResponse = {
      jsonrpc: '2.0',
      id: null,
      error: {
        code: -32700,
        message: 'Parse error'
      }
    };
    process.stdout.write(JSON.stringify(errorResponse));
    return;
  }

  let result = null;
  if (req.method === 'initialize') {
    result = {
      protocolVersion: "2025-06-18",
      capabilities: {
        logging: {},
        prompts: { listChanged: true },
        resources: { subscribe: true, listChanged: true },
        tools: { listChanged: true }
      },
      serverInfo: { name: "ExampleServer", title: "Example Server Display Name", version: "1.0.0" }
    }
  }
  else if (req.method === 'tools/list') {
    result = tools;
  } else if (req.method === 'tools/call') {
    result = utils[req.params.name](req.params.arguments);
  }

  if (result === null) {
    result = {
      error: {
        code: -114514,
        message: 'Method not found'
      }
    }
  }
  const res = {
    jsonrpc: '2.0',
    id: req.id,
    result
  };
  process.stdout.write(JSON.stringify(res));
});